package com.wxl.xueyuan.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.Toast;

import com.wxl.xueyuan.MyApp;
import com.wxl.xueyuan.R;
import com.wxl.xueyuan.api.FileUtils;
import com.wxl.xueyuan.api.Api;
import com.wxl.xueyuan.api.MCallback;
import com.wxl.xueyuan.util.ActivityCollectorUtil;

import java.util.HashMap;


public class DetailActivity extends AppCompatActivity {

    public Context mContext;
    private WebView wv;
    private ImageButton back;
    private ImageButton download;
    private ImageButton collection;
    private ImageButton thumbUp;
    private int colletionFlag = 0;
    private int thumbUpFlag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ActivityCollectorUtil.addActivity(DetailActivity.this);
        mContext = this;

        wv = findViewById(R.id.wv);
        back = findViewById(R.id.back);
        download = findViewById(R.id.download);
        collection = findViewById(R.id.collection);
        thumbUp = findViewById(R.id.thumb_up);

        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        String recordId = bundle.getString("record_id");
        String title = bundle.getString("title");
        String publishedAt = bundle.getString("published_at");
        String rawHtml = bundle.getString("raw_html");
        String thumbUpState = bundle.getString("thumb_up_state");
        String collectionState = bundle.getString("collection_state");

        StringBuffer sb = new StringBuffer();
        //添加html
        sb.append("<html><head><meta http-equiv='content-type' content='text/html; charset=utf-8'>");
        sb.append("<meta charset='utf-8'  content='1'></head><body style='color: black'><p></p>");
        //添加文件标题等信息
        sb.append("<h2 align=\"center\">").append(title).append("</h2>\n").append("    <div align=\"center\">\n").append("        <span>日期：").append(publishedAt).append("</span>\n").append("    </div>\n").append("  <br />");
        //加载本地文件
        // sb.append("<img src='file:///"+AContext.getFileUtil().getDownloadsPath()+"'>");
        //添加文件内容
        sb.append(rawHtml);
        sb.append("</body></html>");
        // webView.loadData(data, mimeType, encoding);
        //设置字符编码，避免乱码
        wv.getSettings().setDefaultTextEncodingName("utf-8");
        wv.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
        //根据标志设置点赞、收藏状态
        if(thumbUpState.equals("1")) {
            thumbUpFlag = 1;
            thumbUp.setImageResource(R.drawable.thumb_up_solid);
        }
        if(collectionState.equals("1")) {
            colletionFlag = 1;
            collection.setImageResource(R.drawable.collection_solid);
        }

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailActivity.super.onBackPressed();
            }
        });

        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //下载
                FileUtils.writeTxtToFile(sb.toString(),"/sdcard/xueyuan/documents/",title+".html");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HashMap<String, Object> params = new HashMap<>();
                        params.put("ID", MyApp.getmData("ID"));
                        params.put("recordId", recordId);
                        Api.config("/article/addDownload", params).getRequest(DetailActivity.this, new MCallback() {
                            @Override
                            public void onSuccess(String res) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(DetailActivity.this, "下载成功,保存至/sdcard/xueyuan/documents/"+title+".html", Toast.LENGTH_LONG).show();
                                    }
                                });
                            }

                            @Override
                            public void onFailure(Exception e) {

                            }
                        });

                    }
                }).start();
            }
        });

        collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //收藏
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(colletionFlag == 0) {
                            colletionFlag = 1;
                            collection.setImageResource(R.drawable.collection_solid);
                        } else {
                            collection.setImageResource(R.drawable.collection_hollow);
                            colletionFlag = 0;
                        }
                        HashMap<String, Object> params = new HashMap<>();
                        params.put("ID", MyApp.getmData("ID"));
                        params.put("recordId", recordId);
                        Api.config("/article/addCollection", params).getRequest(DetailActivity.this, new MCallback() {
                            @Override
                            public void onSuccess(String res) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if(colletionFlag == 0) {
                                            Toast.makeText(DetailActivity.this, "取消收藏成功", Toast.LENGTH_LONG).show();
                                        } else {
                                            Toast.makeText(DetailActivity.this, "收藏成功", Toast.LENGTH_LONG).show();

                                        }

                                    }
                                });
                            }

                            @Override
                            public void onFailure(Exception e) {

                            }
                        });
                    }
                }).start();
            }
        });

        thumbUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点赞
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(thumbUpFlag == 0) {
                            thumbUpFlag = 1;
                            thumbUp.setImageResource(R.drawable.thumb_up_solid);
                        } else {
                            thumbUp.setImageResource(R.drawable.thumb_up_hollow);
                            thumbUpFlag = 0;
                        }
                        HashMap<String, Object> params = new HashMap<>();
                        params.put("ID", MyApp.getmData("ID"));
                        params.put("recordId", recordId);
                        Api.config("/article/addThumb", params).getRequest(DetailActivity.this, new MCallback() {
                            @Override
                            public void onSuccess(String res) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if(colletionFlag == 0) {
                                            Toast.makeText(DetailActivity.this, "取消点赞成功", Toast.LENGTH_LONG).show();
                                        } else {
                                            Toast.makeText(DetailActivity.this, "点赞成功", Toast.LENGTH_LONG).show();

                                        }

                                    }
                                });
                            }

                            @Override
                            public void onFailure(Exception e) {

                            }
                        });
                    }
                }).start();
            }
        });
    }
}
